﻿Ext.define("PSI.MassageInfo.Massage", {
    createToolButtons: function () {
        var me = this;
        var result = [];
        result.push("-");
        result.push({ text: "发布通知", iconCls: "psi-button-add", handler: function () { me.onAddArcitle(null) }, scope: me });

        //result.push("-");
        //result.push({ text: "服务评价", iconCls: "psi-button-comments", handler: me.onComments, scope: me });

        result.push("-");
        result.push({ text: "编辑通知", iconCls: "psi-button-edit", handler: me.onEdithEssaytype, scope: me });
        result.push("-");
        result.push({ text: "删除通知", iconCls: "psi-button-delete", handler: me.onDeleteEssayType, scope: me });
        result.push("-");
        result.push({
            text: "关闭", iconCls: "psi-button-exit", handler: function () {
                location.replace("/");
            }
        });
        return result;
    },

    createUI: function () {
        var me = this;

        var panel = Ext.create("Ext.panel.Panel", {
            border: 0,
            layout: "border",
            title: "通知",
            tbar: me.createToolButtons(),
            items: [
                {
                    region: "center", xtype: "container", layout: "fit", border: false,
                    items: [me.getAcrItems()]
                }
            ]
        });

        me.panel = panel;

    },

    getAcrItems: function () {
        me = this;
        Ext.define("acrmodel", {
            extend: "Ext.data.Model",
            fields: ["Id", "Title", "Contents", "ExpriatinDate", "File1", "File2", "ReleasDate", "ReleasePrason"]

        })
        Ext.create("Ext.data.Store", {
            model: "acrmodel",
            storeId: "acrstore",
            proxy: {
                type: 'ajax',
                url: '/Massage/GetMassageItems',
                extraParams: {},
                actionMethods: { read: 'POST' },
                reader: {
                    type: 'json',
                    root: 'Data'
                }
            },
            autoLoad: true

        })

        return Ext.create("Ext.grid.Panel", {
            viewConfig: { loadingText: "数据加载中...." },
            id: "kthemeitem",
            store: Ext.data.StoreManager.lookup("acrstore"),
            columnLines: true,
            columns: [
                   Ext.create("Ext.grid.RowNumberer", { text: "序号", width: "3%", align: "center" }),
                   { header: '', dataIndex: 'id', hidden: true },
                   { header: "通知标题", dataIndex: 'Title', menuDisabled: true, width: "20%" },
                   {
                       header: "附件1", dataIndex: 'File1', menuDisabled: true, width: "12%",
                       renderer: function (value) {
                           if (value && value.toString() != "") {
                               var Array = value.split('_');
                               return Array[1].toString();
                           }
                       }
                   },
                   {
                       header: "附件2", dataIndex: 'File2', menuDisabled: true, width: "12%",
                       renderer: function (value) {
                           if (value && value.toString() != "") {
                               var Array = value.split('_');
                               return Array[1].toString();
                           }
                       }
                   }, {
                       header: "过期时间", dataIndex: 'ExpriatinDate', menuDisabled: true, width: "8%",
                       renderer: function (value) {
                           if (value) {
                               var dt = eval("new " + value.substr(1, value.length - 2)).toString();
                               return Ext.util.Format.date(dt, "Y-m-d ");//"Y年m月d日H时i分s秒"  

                           }
                       }
                   },
                   {
                       header: "创建时间", dataIndex: 'ReleasDate', menuDisabled: true, width: "8%",
                       renderer: function (value) {
                           if (value) {
                               var dt = eval("new " + value.substr(1, value.length - 2)).toString();
                               return Ext.util.Format.date(dt, "Y-m-d H:i");//"Y年m月d日H时i分s秒"  

                           }
                       }
                   },
                   { header: "创建人", dataIndex: 'ReleasePrason', menuDisabled: true, width: "10%" }
                  // { header: "类别名称", dataIndex: 'name', menuDisabled: true, width: "84%" },
            ],
            listeners: {
                itemdblclick: function (view, record, item, index, e, eOpts) {
                    me.onAddArcitle(record.raw);

                }
            }

        })

    },

    getMainPanel: function () {
        return this.panel;
    },

    onAddArcitle: function (entity) {
        var f1, f2, ExpriatinDate;
        if (entity != null) {
            if (entity.File1 != "") {
                var cc = entity.File1.split("_");
                f1 = cc[1];
            }
            if (entity.File2 != "") {
                var cc = entity.File2.split("_");
                f2 = cc[1];
            }
        }
        var windowArcitle = Ext.create("Ext.window.Window", {
            modal: true,
            id: 'arcform',
            title: entity == null ? "发布通知" : "编辑通知",
            width: 700,
            height: 430,
            layout: "fit",
            items: [
                {
                    xtype: 'form',
                    id: 'arcitlewin',
                    border: 0,
                    reset :true,
                    layout: "vbox",
                    //    collapsible: true,
                    frame: true,
                    defaultType: 'textfield',
                    fieldDefaults: {
                        labelAlign: 'right',
                        labelWidth: 70,
                        msgTarget: 'side'
                    },
                    bodyPadding: '5 5 0',
                    items: [
                        {
                            id: "themeid",
                            xtype: "hidden",
                            name: "Id",
                            value: entity == null ? null : entity.Id
                        },
                        {
                            fieldLabel: '通知标题',
                            name: 'Title',
                            value: entity == null ? "" : entity.Title  ,
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            allowBlank: false,
                            width: 650,
                            anchor: '100%'
                        }, {
                            xtype: 'container',
                            layout: 'hbox',
                            width: 500,
                            margin: '0 5 5 0',
                            items: [{
                                xtype: 'filefield',
                                fieldLabel: '添加附件1',
                                name: 'file1',
                                buttonOnly: true,
                                buttonText: "浏览文件",
                                flex: 1,
                                listeners: {
                                    'change': function (fb, v) {
                                        var f = Ext.getCmp("arcitlewin");
                                        var el = f.getEl();
                                        var index = v.split("\\");

                                        var parm = {
                                            filename: PSI.Const.NewGuid + "_" + index[(index.length) - 1],
                                            filetype: 1
                                        }
                                        el.mask("文件保存中...");
                                        f.submit({
                                            url: "/Massage/UploadFile",
                                            method: "POST",
                                            params: parm,
                                            clientValidation:false,
                                            success: function (form, action) {
                                                el.unmask();
                                                Ext.getCmp("f_one").setText(index[(index.length) - 1]);
                                                Ext.getCmp("readyone").setValue(parm.filename);
                                            },
                                            failure: function (form, action) {
                                                 el.unmask();
                                            }
                                        });
                                    }
                                }
                            }, {
                                xtype: 'label',
                                id: 'f_one',
                                text: f1,
                                flex: 2

                            }, {
                                xtype: 'hidden',
                                id: 'readyone',
                                value: entity == null ? "" : entity.File1,
                                name: 'File1'
                            }]
                        }, {
                            xtype: 'container',
                            layout: 'hbox',
                            width: 500,
                            margin: '0 5 5 0',
                            items: [{
                                xtype: 'filefield',
                                fieldLabel: '添加附件2',
                                name: 'file2',
                                buttonOnly: true,
                                buttonText: "浏览文件",
                                flex: 1,
                                listeners: {
                                    'change': function (fb, v) {
                                        var f = Ext.getCmp("arcitlewin");
                                        var el = f.getEl();
                                        el.mask();
                                        var index = v.split("\\");
                                        var parm = {
                                            filename: PSI.Const.NewGuid + "_" + index[2],
                                            filetype: 2
                                        }
                                        el.mask("文件保存中...");
                                        f.submit({
                                            url: "/Massage/UploadFile",
                                            method: "POST",
                                            params: parm,
                                            clientValidation: false,
                                            success: function (form, action) {
                                                el.unmask();
                                                Ext.getCmp("f_two").setText(index[2]);
                                                Ext.getCmp("readytwo").setValue(parm.filename);
                                            },
                                            failure: function (form, action) {
                                                el.unmask();
                                                PSI.MsgBox.showInfo("服务器或网络问题,文件上传失败。", function () {

                                                });
                                            }
                                        });
                                    }
                                }
                            }, {
                                xtype: 'label',
                                id: 'f_two',
                                text: f2,
                                flex: 2
                            }, {
                                xtype: 'hidden',
                                id: 'readytwo',
                                value: entity == null ? "" : entity.File2,
                                name: 'File2'
                            }]
                        }, {
                            xtype: 'datefield',
                            format: "Y-m-d",
                            id: "etype",
                            fieldLabel: '截止日期',
                            name: 'ExpriatinDate',
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            value: entity == null ? "" : "2013-12-01",
                            allowBlank: false,
                            width: 250,
                            anchor: '100%'
                        }, {
                            xtype: 'htmleditor',
                            name: 'Contents',
                            fieldLabel: '通知内容',
                            beforeLabelTextTpl: PSI.Const.REQUIRED,
                            allowBlank: false,
                            value: entity == null ? "" : entity.Contents,
                            height: 240,
                            width: 650,
                            anchor: '100%'
                        }

                    ]
                }

            ], buttons: [
                {
                    text: entity == null ? "发布" : "编辑",
                    formBind: true,
                    iconCls: "psi-button-ok",
                    handler: this.releayOK,
                    scope: this
                },
                {
                    text: "取消", handler: function () {
                        windowArcitle.close();
                    }, scope: this
                }
            ]

        }).show();
        this.windowArcitle = windowArcitle;
        if (entity != null) {
           var dt = eval("new " + (entity.ExpriatinDate).substr(1, (entity.ExpriatinDate).length - 2)).toString();
           ExpriatinDate = Ext.util.Format.date(dt)
           Ext.getCmp("etype").setValue(ExpriatinDate);
        }
    },

    onDeleteEssayType: function () {
        me = this;
        var grid = Ext.getCmp("kthemeitem");
        var items = grid.getSelectionModel().getSelection();
        if (!items || items.length == 0) {
            PSI.MsgBox.showInfo("请选择一条要删除的通知！");
            return;
        }
        PSI.MsgBox.confirm("确定是否要删除记录！", function () {
            var iden = items[0].getData().Id;
            var el = Ext.getBody();
            el.mask("正在删除中...");
            Ext.Ajax.request({
                url: "/Massage/DelMassage",
                method: "POST",
                params: { id: iden },
                callback: function (options, success, response) {
                    el.unmask();
                    if (success) {
                        var data = Ext.JSON.decode(response.responseText);
                        if (data.success) {
                            PSI.MsgBox.showInfo("成功完成删除操作", function () {
                                var store = Ext.data.StoreManager.lookup("acrstore");
                                store.load();
                            });
                        } else {
                            PSI.MsgBox.showInfo("网络错误");
                        }
                    } else {
                        PSI.MsgBox.showInfo("网络错误");
                    }
                }

            });

        })
    },

    releayOK: function () {
        me = this;
        var f = Ext.getCmp("arcitlewin");
        var el = f.getEl();
        f.submit(
        {
            url: "/Massage/ReleaseMassage",
            method: "POST",
            success: function (from, action) {
                el.unmask();
                var info;
                if (Ext.getCmp("themeid").getValue() != "") {
                    info = "通知编辑完成！";
                }
                else {
                    info = "通知发布成功！";
                }
                PSI.MsgBox.showInfo(info, function () {
                    
                    var store = Ext.data.StoreManager.lookup("acrstore");
                    store.load();
                    me.windowArcitle.close();
                });
            }
        })

    },

    onEdithEssaytype: function () {
        var grid = Ext.getCmp("kthemeitem");
        var items = grid.getSelectionModel().getSelection();
        if (!items || items.length == 0) {
            PSI.MsgBox.showInfo("请选择一条要编辑的通知！");
            return;
        }

        this.onAddArcitle(items[0].getData());
    },
});